// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import easing = cc.easing;

const {ccclass, property} = cc._decorator;

@ccclass
export default class FlyPower extends cc.Component {

    //初始点
    initPos = null;
    //结束点
    endPos = null;
    //回调
    cb = null;
    //第一个贝塞尔点
    bezierPos1 = null;
    //第二个贝塞尔点
    bezierPos2 = null;

    start () {

    }

    init(pos1, pos2, cb, add?){
        let rand = Math.random() < 0.5 ? 0 : 1;

        let x = (rand == 1 ? (Math.abs(pos2.x - pos1.x)/4 * 5) : (-Math.abs(pos2.x - pos1.x))) + (pos2.x > pos1.x ? pos1.x : pos2.x);
        let y = Math.abs(pos2.y - pos1.y)/4 * 3 + (pos2.y > pos1.y ? pos1.y : pos2.y);
        let y1 = Math.abs(pos2.y - pos1.y)/4 + (pos2.y > pos1.y ? pos1.y : pos2.y);
        this.bezierPos1 = cc.find("Canvas").convertToNodeSpaceAR(cc.v2(x,y));
        this.bezierPos2 = cc.find("Canvas").convertToNodeSpaceAR(cc.v2(x,y1));

        if(add){
            let pos = this.bezierPos1;
            this.bezierPos1 = this.bezierPos2;
            this.bezierPos2 = pos;
        }

        this.initPos = cc.find("Canvas").convertToNodeSpaceAR(pos1);
        this.endPos = cc.find("Canvas").convertToNodeSpaceAR(pos2);
        this.cb = cb;
        this.node.setPosition(this.initPos);
        this.show();
    }

    show(){
        cc.tween(this.node)
            .bezierTo(0.5, this.bezierPos1, this.bezierPos2,this.endPos)
            .call(()=>{
                this.cb();
                this.node.destroy()
            })
            .start()
    }

    // update (dt) {}
}
